package com.lanjiu.lib.utils.log;

import android.text.TextUtils;
import android.util.Log;

import java.util.Collection;
import java.util.Map;

/**
 * @description: Log 工具类
 * @author: Panwei
 * @date: 2019/10/28 19:35
 */
public class Logger {

    private static final String TAG = "IM-LOG";
    private static final boolean DEBUG = true;

    private Logger() {
    }

    public static void print(String msg) {
        if (DEBUG && checkNotNull(TAG, msg)) {
            printLog(TAG, msg);
        }
    }

    public static void print(String tag, String msg) {
        if (DEBUG) {
            String thisTag = TAG + "[" + tag + "]";
            if (checkNotNull(thisTag, msg)) {
                printLog(thisTag, msg);
            }
        }
    }

    public static void print(Object obj) {
        if (DEBUG) {
            print(String.valueOf(obj));
        }
    }

    public static void print(String tag, Object obj) {
        if (DEBUG) {
            print(tag, obj.toString());
        }
    }

    public static <E> void print(Collection<E> collection) {
        if (DEBUG) {
            print(convertCollectionToString(collection));
        }
    }

    public static <E> void print(String tag, Collection<E> collection) {
        if (DEBUG) {
            print(tag, convertCollectionToString(collection));
        }
    }

    public static <K, V> void print(Map<K, V> map) {
        if (DEBUG) {
            print(convertMapToString(map));
        }
    }

    public static <K, V> void print(String tag, Map<K, V> map) {
        if (DEBUG) {
            print(tag, convertMapToString(map));
        }
    }

    private static <K, V> String convertMapToString(Map<K, V> map) {
        StringBuilder builder = new StringBuilder("[");
        builder.append(map.getClass().getName());
        builder.append("]");
        for (Map.Entry<K, V> entry : map.entrySet()) {
            builder.append("\n[");
            builder.append(entry.getKey().toString());
            builder.append("=");
            builder.append(entry.getValue().toString());
            builder.append("]");
        }
        return builder.toString();
    }

    private static <E> String convertCollectionToString(Collection<E> collection) {
        if (collection == null || collection.size() <= 0)
            return null;
        StringBuilder builder = new StringBuilder("[");
        builder.append(collection.getClass().getName());
        builder.append("]");
        for (E e : collection) {
            builder.append("\n[");
            builder.append(e.toString());
            builder.append("]");
        }
        return builder.toString();
    }

    private static boolean checkNotNull(String tag, String msg) {
        if (TextUtils.isEmpty(msg)) {
            Log.d(tag, "Logger 'msg' is null");
            return false;
        }
        return true;
    }

    private static void printLog(String tag, String msg) {
        // MAX字节打印长度(字节) : 4*1024
        int max_byte_length = 4 * 1024 - tag.getBytes().length;
        int length = msg.getBytes().length;
        if (length < max_byte_length) {
            Log.d(tag, msg);
        } else {
            //大于max_str_length时， 防止中文过多导致丢失
            int max_str_length = 2 * 1024 - tag.length();
            while (msg.length() > max_str_length) {
                Log.d(tag, msg.substring(0, max_str_length));
                msg = msg.substring(max_str_length);
            }
            if (msg.length() > 0)
                Log.d(tag, msg);
        }
    }
}
